/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：BaseMenu.js<br>
 * 日期：2016年12月8日<br>
 * 作者: 杨顺强<br>
 * 功能：菜单的基础类。完成数据的初始化，和菜单项基本功能的实现。不包含对应菜单的创建功能<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");


/**
 * 类型名称：Rainier.UI.V2.CanvasMenu<br>
 * 类型功能：右键菜单对象<br>
 */
Rainier.UI.V2.BaseMenu = Rainier.Basic.CompositeObject.extend({
	
	NAME : "Rainier.UI.V2.BaseMenu",
	
	init : function(canvas,canvasEditPart,positionX,positionY){
		var _this		= this;
		this.canvas 	= canvas;
		this.editPart	= canvasEditPart;
		this.rootModel	= this.canvas.context.getRootModel();
		this.positionX  = positionX;
		this.positionY  = positionY;
		var commandStack      = this.editPart.getFigure().getCommandStack();
		var undoStackSize     = commandStack.undostack.length;
		var redoStackSize     = commandStack.redostack.length;
		/**
		 * menuItemUsedInfo 默认值
		 * {
		 *		deviceInfoUsed	: true,
		 *		deviceShowAll   : true,
		 *		guideLineUsed	: false,
		 *		jointPortUsed	: false,
		 *		copyUsed		: false
		 * };
		 */
		this.menuItemUsedInfo = this.canvas.context.menuItemUsedInfo;
		
		var selected  = 0;
		var copyModel = 0;
		selected  = this.canvas.context.getSelected().getSize();
		copyModel = this.canvas.context.copyModelList.getSize();
		
		
		this.items 					= {};
//		this.items.dotBackground 	= {name:"<span>点格</span><span></span>",isHtmlName:true,icon:"dotBackground"};
//		this.items.gridBackground 	= {name:"<span>网格</span><span></span>",isHtmlName:true,icon:"gridBackground"};
//		this.items.blankBackground 	= {name:"<span>空白</span><span></span>",isHtmlName:true,icon:"blankBackground"};
//		this.items.deviceInfo		= {name:"<span>器材信息（显示/隐藏）</span><span></span>",isHtmlName:true,icon:"deviceInfo"};
//		this.items.jointPort 		= {name:"<span>节点信息（显示/隐藏）</span><span></span>",isHtmlName:true,icon:"jointPort"};
		/*this.items.split1			="-----------------";*/
		this.items.copy			    = {name:"<span>复制</span><span>Ctrl+C</span>",isHtmlName:true,icon:(selected > 0)?"copy":"copy1",
											disabled:function(itemKey,opt){
												if(selected > 0){
													return false;
												}
												return true
											}};
		this.items.cut			    = {name:"<span>剪切</span><span>Ctrl+X</span>",isHtmlName:true,icon:(selected > 0)?"cut":"cut1",
											disabled:function(itemKey,opt){
												if(selected > 0){
													return false;
												}
												return true
											}};
		this.items.paste			= {name:"<span>粘贴</span><span>Ctrl+V</span>",isHtmlName:true,
											options:{copyUsed:_this.menuItemUsedInfo.copyUsed},
											icon:function(opt, $itemElement, itemKey, item){
												if(copyModel > 0){
													return 'context-menu-icon context-menu-icon-paste';
												}
												return 'context-menu-icon context-menu-icon-paste1';
											},
											disabled:function(itemKey,opt){
												if(copyModel > 0){
													return false;
												}
												return true
											}
									  };
		this.items.redo				= {name:"<span>恢复</span><span>Ctrl+Y</span>",isHtmlName:true,
											options:{redoSize : redoStackSize},
											icon:function(opt, $itemElement, itemKey, item){
												if(item.options.redoSize>0){
													return 'context-menu-icon context-menu-icon-redo';
												}
												return 'context-menu-icon context-menu-icon-redo1';
												
											},
											disabled:function(itemKey,opt){
												var options = opt.items[itemKey].options;
												if(options.redoSize){
													return false;
												}
												return true;
											}
									  };
		this.items.undo				= {name:"<span>撤销</span><span>Ctrl+Z</span>",isHtmlName:true,
											options:{undoSize : undoStackSize},
											icon:function(opt, $itemElement, itemKey, item){
												if(item.options.undoSize>0){
													return 'context-menu-icon context-menu-icon-undo';
												}
												return 'context-menu-icon context-menu-icon-undo1';
											},
											disabled:function(itemKey,opt){
												var options = opt.items[itemKey].options;
												if(options.undoSize){
													return false;
												}
												return true;
											}
									  };

		this.items._delete			= {name:"<span>删除</span><span>Delete</span>",isHtmlName:true,icon:"delete1",disabled:true};
		/*this.items.split3			="-----------------";*/
//		this.items.zoomIn			= {name:"<span>放大</span><span></span>",isHtmlName:true,icon:"zoomIn"};
//		this.items.zoomOut			= {name:"<span>缩小</span><span></span>",isHtmlName:true,icon:"zoomOut"};
//		this.items.zoomToFit		= {name:"<span><span class='leftBorder'>还原</span><span></span></span>",isHtmlName:true,icon:"restore"};
		/*this.items.split4			="-----------------";*/
		this.items.clearTable		= {name:"<span>清空实验台</span><span></span>",isHtmlName:true,icon:"clear-ExprimentTable"};
		this.items._delete			= {name:"<span>删除</span><span>Delete</span>",isHtmlName:true,icon:"delete1",disabled:true};
		/*this.items.split3			="-----------------";*/
//		this.items.zoomIn			= {name:"<span>放大</span><span></span>",isHtmlName:true,icon:"zoomIn"};
//		this.items.zoomOut			= {name:"<span>缩小</span><span></span>",isHtmlName:true,icon:"zoomOut"};
//		this.items.zoomToFit		= {name:"<span>还原</span><span></span>",isHtmlName:true,icon:"restore"};
		/*this.items.split4			="-----------------";*/
		this.items.clearTable		= {name:"清空实验台",icon:"clear-ExprimentTable"};
		this.items._delete			= {name:"<span class='leftBorder'>删除</span><span>Delete</span>",isHtmlName:true,icon:(selected > 0)?"delete":"delete1",
											disabled:function(itemKey,opt){
												if(selected > 0){
													return false;
												}
												return true
											}
									  };
		/*this.items.split3			="-----------------";*/
//		this.items.zoomIn			= {name:"<span>放大</span><span></span>",isHtmlName:true,icon:"zoomIn"};
//		this.items.zoomOut			= {name:"<span>缩小</span><span></span>",isHtmlName:true,icon:"zoomOut"};
//		this.items.zoomToFit		= {name:"<span>还原</span><span></span>",isHtmlName:true,icon:"restore"};
		/*this.items.split4			="-----------------";*/
		this.items.clearTable		= {name:"<span>清空实验台</span>",isHtmlName:true,icon:"clear-ExprimentTable"};

	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.show<br>
	 * 函数功能：显示创建的菜单<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	show : function(){
		var activeEditor   = app.getContext().getActiveEditor();
		var status  	   = activeEditor.getRunStatus();
		var _this		   = this;
		$.contextMenu({  
            selector : 'body', 
            autoHide : true,
            callback : $.proxy(function(key,options){
            	if(status){
            		return;
            	}
            	switch(key){
            	case "dotBackground":
            		this.dotBackground();
        			break;
            	case "gridBackground":
            		this.gridBackground();
        			break;
            	case "blankBackground":
            		this.blankBackground();
        			break;
            	case "deviceInfo":
            		this.deviceInfo();
        			break;
            	case "jointPort":
            		this.jointPort();
        			break;
            	case "copy":
            		this.copy();
        			break;
            	case "cut":
            		this.cut();
            		break;
            	case "paste":
            		this.paste();
        			break;
            	case "redo":
            		this.redo();
            		break;
            	case "undo":
            		this.undo();
            		break;
            	case "_delete":
            		this._delete();
            		break;
            	case "zoomIn":
            		this.zoomIn();
        			break;
            	case "zoomOut":
            		this.zoomOut();
        			break;
            	case "zoomToFit":
            		this.zoomToFit();
        			break;
            	case "clearTable":
            		this.clearTable();
        			break;
        		default:
        			break;
            	}			
            },this),
            events:
            {  
                hide:function(){
                	_this.canvas.setCurrentSelection(null);
                	$.contextMenu( 'destroy' ); 
            	}
            },
            x:this.positionX,
            y:this.positionY,
            items: this.items,
            zIndex:1000
        });  
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.dotBackGround<br>
	 * 函数功能：修改帆布显示样式为 点格的样式<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	dotBackground : function(){
		this.editPart.dotBackground();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.gridBackground<br>
	 * 函数功能：修改帆布显示样式为 网格的样式<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	gridBackground : function(){
		this.editPart.gridBackground();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.blandBackGround<br>
	 * 函数功能：修改帆布显示样式为 空白的样式<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	blankBackground : function(){
		this.editPart.blankBackground();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.guideLine<br>
	 * 函数功能：<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	guideLine : function(){
		var snapToGeometry 	= this.editPart.editPolicySnapToGeometry;
		var snapToCenter 	= this.editPart.editPolicySnapToCenter;
		var snapToInBetween = this.editPart.editPolicySnapToInBetween;
		if(!this.menuItemUsedInfo.guideLineUsed){
			this.canvas.uninstallEditPolicy(snapToGeometry);
			this.canvas.uninstallEditPolicy(snapToCenter);
			this.canvas.uninstallEditPolicy(snapToInBetween);
			
			this.menuItemUsedInfo.guideLineUsed		= true;
		}else{
			this.canvas.installEditPolicy(snapToGeometry);
			this.canvas.installEditPolicy(snapToCenter);
			this.canvas.installEditPolicy(snapToInBetween);
			this.menuItemUsedInfo.guideLineUsed		= false;
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.deviceInfo<br>
	 * 函数功能：<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	deviceInfo : function(){
		var _this = this;
		var childrenModels = this.rootModel.getChildren();
		if(this.rootModel && this.rootModel.hasChildren()){
			if(!_this.menuItemUsedInfo.deviceInfoUsed){
				childrenModels.forEach(function(model,index){
					model.showAllLabel();
				});
				_this.menuItemUsedInfo.deviceInfoUsed = true;
				_this.menuItemUsedInfo.deviceShowAll  = true; 
			}else{
				childrenModels.forEach(function(model,index){
					model.hideAllLabel();
				});
				_this.menuItemUsedInfo.deviceInfoUsed = false;
				_this.menuItemUsedInfo.deviceShowAll  = false; 
			}
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.jointPort<br>
	 * 函数功能：<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	jointPort : function(){
		var _this = this;
		if(this.rootModel && this.rootModel.hasChildren()){
			//TODO:显示连线节点
			var connections = this.rootModel.getConnections();
			if(!_this.menuItemUsedInfo.jointPortUsed){
				connections.each(function(index,model){
					model.hideNodeLabel(true,false);
				});
				_this.menuItemUsedInfo.jointPortUsed = true;
			}else{
				_this.menuItemUsedInfo.jointPortUsed = false;
//				this.canvas.context.circuitparser.parse();
				var start = new Date().getTime();
				connections.each(function(index,model){
//					model.changeNodeLabel(true,false);
					model.showNodeLabel(true,false);
				});
				var end = new Date().getTime();
				console.log("add model cost:" + (end - start));
			}
		}
		
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.copy<br>
	 * 函数功能：<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	copy : function(){
		var _this = this;
		var selected = this.canvas.context.getSelected();
    	var editParts = new draw2d.util.ArrayList();
			selected.clone().each(function(j,select){
    		var editPart = _this.canvas.context.findEditPartByModel(select);
    		if(editPart){
    			editParts.add(editPart);
    		}
        })
		var cmd = new Rainier.UI.V2.CommandCopy(this.canvas.context,editParts);
		this.canvas.getCommandStack().execute(cmd);
	},

	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.cut<br>
	 * 函数功能：<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	cut : function(){
		var _this = this;
		var selected = this.canvas.context.getSelected();
    	var editParts = new draw2d.util.ArrayList();
			selected.clone().each(function(j,select){
    		var editPart = _this.canvas.context.findEditPartByModel(select);
    		if(editPart){
    			editParts.add(editPart);
    		}
        })
		var cmd = new Rainier.UI.V2.CommandCut(this.canvas.context,editParts);
		this.canvas.getCommandStack().execute(cmd);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.paste<br>
	 * 函数功能：完成当前对象的粘贴命令<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	paste : function(){
		var stack 	= this.canvas.getCommandStack();
  		var cmd 	= new Rainier.UI.V2.CommandPaste(this.canvas.context);
	    stack.execute(cmd);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.redo<br>
	 * 函数功能：恢复到上一步操作<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	redo : function(){
		var stack 	= this.canvas.getCommandStack();
		stack.redo();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.undo<br>
	 * 函数功能：撤销上一步操作<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	undo : function(){
		var stack 	= this.canvas.getCommandStack();
		stack.undo();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu._delete<br>
	 * 函数功能：<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	_delete : function(){
		var _this = this;
		var figures   = this.canvas.getSelection().all;
		this.canvas.getCommandStack().startTransaction(draw2d.Configuration.i18n.command.deleteShape);
		figures.each(function(index, figure){
           var cmd = figure.createCommand(new draw2d.command.CommandType(draw2d.command.CommandType.DELETE));
           if(cmd!==null){
        	   _this.canvas.getCommandStack().execute(cmd);
           }
       });
       // execute all single commands at once.
		this.canvas.getCommandStack().commitTransaction();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.zoomIn<br>
	 * 函数功能：放大帆布<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	zoomIn : function(){
		var zoomInAction = new Rainier.UI.V2.ZoomInAction();
		zoomInAction.execute();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.zoomOut<br>
	 * 函数功能：缩小帆布<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	zoomOut : function(){
		var zoomOutAction = new Rainier.UI.V2.ZoomOutAction();
		zoomOutAction.execute();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.zoomToFit<br>
	 * 函数功能：还原帆布<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	zoomToFit : function(){
		var zoomToFit = new Rainier.UI.V2.RestoreAction();
		zoomToFit.execute();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CanvasMenu.clearTable<br>
	 * 函数功能：清空试验台<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	clearTable : function(){
		var _this = this;
		var figures   = this.canvas.getFigures();
		this.canvas.getCommandStack().startTransaction(draw2d.Configuration.i18n.command.deleteShape);
		figures.each(function(index, figure){
           var cmd = figure.createCommand(new draw2d.command.CommandType(draw2d.command.CommandType.DELETE));
           if(cmd!==null){
        	   _this.canvas.getCommandStack().execute(cmd);
           }
       });
       // execute all single commands at once.
		this.canvas.getCommandStack().commitTransaction();
	},
	
});